import 'package:flutter/widgets.dart';

import 'entity.dart';

class WordPainter extends CustomPainter {
  List<WordItem> list;
  BuildContext context;
  var currentHeight = 0.0;

  WordPainter(this.context, this.list);

  @override
  void paint(Canvas canvas, Size size) {
    print("size = $size");
    currentHeight = 0;
    for (var item in list) {
      if (item is TextItem) {
        print(item);
        var layoutSize = item.layout(context, size.width, double.infinity);
        _paintText(item, currentHeight, layoutSize, canvas);
        currentHeight += layoutSize.height;
      }
    }

    print(currentHeight);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;

  void _paintText(
      TextItem item, double currentHeight, Size layoutSize, Canvas canvas) {
    var textPainter = TextPainter(textDirection: item.direction);
    textPainter.text = item.text;
    textPainter.layout(maxWidth: layoutSize.width);
    textPainter.paint(canvas, Offset(0.0, currentHeight));
  }
}
